package nlp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;

public class FreqDistUtil {
	private List<FreqData> freqList;

	private void MedianNormalize() {
		int medianPos = freqList.size() / 2;
		int posIdx = 0;
		double median = 0.0;
		// Find the median value.
		for (FreqData freqData : freqList) {
			if (posIdx == medianPos) {
				median = freqData.getFrequency();
			}
			++posIdx;
		}
		// Normalize using X/(X+Median)
		for (FreqData freqData : freqList) {
			double freq = freqData.getFrequency();
			freqData.setFrequency(freq / (freq + median));
		}
	}

	private void TotalNormalize(int total) {
		for (FreqData freqData : freqList) {
			double freq = freqData.getFrequency();
			freqData.setFrequency((float) freq / (float) total);
		}
	}

	// Constructor.
	public FreqDistUtil(List<String> tokens, int total) {
		HashMap<String, Double> freqHash = new HashMap<String, Double>();
		for (String token : tokens) {
			if (freqHash.containsKey(token)) {
				double currentFreq = freqHash.get(token);
				freqHash.put(token, ++currentFreq);
			} else {
				freqHash.put(token, 1.0);
			}
		}

		freqList = new ArrayList<FreqData>();
		for (String token : freqHash.keySet()) {
			freqList.add(new FreqData(token, freqHash.get(token)));
		}
		Collections.sort(freqList);

		// Normalize the frequencies.
		this.TotalNormalize(total);
	}

	public List<FreqData> getTop(int k) {
		List<FreqData> retList = new ArrayList<FreqData>();
		Iterator<FreqData> freqDataIter = freqList.iterator();
		for (int idx = 0; idx < k && freqDataIter.hasNext(); ++idx) {
			retList.add(freqDataIter.next());
		}
		return retList;
	}

	public List<FreqData> getAll() {
		return this.getTop(freqList.size());
	}
}